<?php

namespace XWAM\Model;

/**
 * ProductGoods 模型类
 *
 * @since 1.0 <2016-5-28> SoChishun <14507247@qq.com> Added.
 */
class ProductGoodsModel extends AppbaseModel {

    protected $tableName = 't_porg_product_goods';

    /**
     * 获取id_text列表
     * @param type $site_id
     * @param type $key_is_id
     * @return type
     * @since 1.0 2016-5-30 SoChishun Added.
     */
    public function getIdTextList($site_id, $key_is_id = false) {
        $list = array();
        if ($key_is_id) {
            $list = $this->where(array('site_id' => $site_id))->cache('productgoods_idtext_kvlist_' . $site_id, 10)->getField('id, title as text');
        } else {
            $list = $this->where(array('site_id' => $site_id))->cache('productgoods_idtext_list_' . $site_id, 10)->field('id, title as text')->select();
        }
        return $list;
    }

    function saveGoods() {
        $rules = array(
            array('title', 'require', '标题无效!', self::MUST_VALIDATE, 'regex', self::MODEL_INSERT),
            array('category_id', 'number', '类别无效!', self::EXISTS_VALIDATE, 'regex', self::MODEL_BOTH),
            array('price', 'currency', '价格无效!', self::EXISTS_VALIDATE, 'regex', self::MODEL_BOTH),
            array('user_name', 'require', '用户无效!', self::EXISTS_VALIDATE, 'regex', self::MODEL_BOTH),
            array('site_id', 'number', 'SiteID无效!', self::EXISTS_VALIDATE, 'regex', self::MODEL_BOTH),
        );
        if (!$this->validate($rules)->create()) {
            return $this->returnMsg(false, $this->getError());
        }
        //图片上传
        $msg = \Common\Controller\UploadHandlerController::upload(array('savePath' => 'ProductGoods/', 'skipEmpty' => true));
        if (is_array($msg) && isset($msg['picture_url'])) {
            $this->data['picture_url'] = $msg['picture_url']['filepath'];
        }
        if (isset($this->data['create_time']) && empty($this->data['create_time'])) {
            unset($this->data['create_time']);
        }
        if (empty($this->id)) {
            unset($this->data['id']);
            $result = $this->add();
        } else {
            $result = $this->save();
        }
        return $this->returnMsg($result);
    }

    function save_specification_goods() {
        $rules = array(
            array('title', 'require', '标题无效!', self::MUST_VALIDATE, 'regex', self::MODEL_INSERT),
            array('pid', 'number', '主商品无效!', self::EXISTS_VALIDATE, 'regex', self::MODEL_BOTH),
            array('price', 'currency', '价格无效!', self::EXISTS_VALIDATE, 'regex', self::MODEL_BOTH),
            array('user_name', 'require', '用户无效!', self::EXISTS_VALIDATE, 'regex', self::MODEL_BOTH),
            array('site_id', 'number', 'SiteID无效!', self::EXISTS_VALIDATE, 'regex', self::MODEL_BOTH),
        );
        if (!$this->validate($rules)->create()) {
            return $this->returnMsg(false, $this->getError());
        }
        //图片上传
        $msg = \Common\Controller\UploadHandlerController::upload(array('savePath' => 'ProductGoods/', 'skipEmpty' => true));
        if (is_array($msg) && isset($msg['picture_url'])) {
            $this->data['picture_url'] = $msg['picture_url']['filepath'];
        } else {
            if (empty($this->id) && !empty($this->picture_url2)) {
                $this->data['picture_url'] = $this->picture_url2;
            }
        }
        unset($this->data['picture_url2']);
        $this->data['specification_code'] = '.' . implode('.', $this->data['specification']) . '.';
        unset($this->data['specification']);
        if (isset($this->data['create_time']) && empty($this->data['create_time'])) {
            unset($this->data['create_time']);
        }
        if (empty($this->id)) {
            unset($this->data['id']);
            $result = $this->add();
        } else {
            $result = $this->save();
        }
        return $this->returnMsg($result);
    }

    /**
     * 获取内容文本
     * @param type $id
     * @param type $code
     * @return type
     * @since 1.0 2016-8-26 SoChishun Added.
     */
    function getContentText($id, $code) {
        $text = $this->table('t_porg_product_content')->where(array('product_id' => $id, 'code' => $code))->getField('content');
        return $text ? html_entity_decode($text) : $text;
    }

    // 2016-7-1
    function findContent($id, $code) {
        $data = $this->table('t_porg_product_content')->where(array('product_id' => $id, 'code' => $code))->find();
        if (!$data) {
            $data = array('product_id' => $id, 'code' => $code);
            $this->table('t_porg_product_content')->add($data);
        }
        return $data;
    }

    function saveContent() {
        $rules = array(
            array('product_id', 'number', '商品编号无效!', self::EXISTS_VALIDATE, 'regex', self::MODEL_BOTH),
            array('code', 'require', '内容代码无效!', self::EXISTS_VALIDATE, 'regex', self::MODEL_BOTH),
        );
        if (!$this->validate($rules)->create()) {
            return $this->returnMsg(false, $this->getError());
        }
        $where = array('product_id' => $this->data['product_id'], 'code' => $this->data['code']);
        $result = $this->table('t_porg_product_content')->where($where)->setField('content', $this->data['content']);
        return $this->returnMsg($result);
    }

    // 2017-3-23
    function changePromotionStatus($id, $status, $type = '') {
        $type = strtolower($type);
        $fields = array('isnew' => 'is_new', 'ishot' => 'is_hot', 'isrecom' => 'is_recommend');
        if (!isset($fields[$type])) {
            return $this->returnMsg(false, '促销类型无效!');
        }
        $result = $this->where(array('id' => $id))->setField(array($fields[$type] => $status));
        return $this->returnMsg($result);
    }

    function changeStatus($id, $status) {
        $result = $this->where(array('id' => $id))->setField(array('status' => $status));
        return $this->returnMsg($result);
    }

    function changeSort($id, $sort) {
        $result = $this->where(array('id' => $id))->setField(array('sort' => $sort));
        return $this->returnMsg($result);
    }

    function deleteGoods($id) {
        $result = $this->delete($id);
        if (false != $result) {
            $this->where(array('pid' => $id))->delete();
        }
        return $this->returnMsg($result);
    }

    function saveCategory($aid, $aCategoryId) {
        $table = $this->tableName;
        $sqls = array();
        $upIds = array();
        $i = 0;
        foreach ($aCategoryId as $categoryId) {
            $id = $aid[$i];
            if ($categoryId) {
                $upIds[] = $categoryId;
                $sqls[] = "update $table set category_id=$categoryId where id=$id";
            }
            $i++;
        }
        if (!$sqls) {
            return $this->returnMsg(false, '未选择类别!');
        }
        $mAdv = new \Think\Model\AdvModel();
        $mAdv->patchQuery($sqls);
        ProductCategoryModel::updateForeignTitle(false, implode(',', $upIds));
        return $this->returnMsg(true);
    }
    // 2017-3-23
    function savePromotion($data) {
        $table = $this->tableName;
        $sqls = array();
        $upIds = array();
        foreach ($data as $id => $promotionIds) {
            $sqls[] = "update $table set promotion_ids='$promotionIds' where id=$id";
        }
        if (!$sqls) {
            return $this->returnMsg(false, '空记录!');
        }
        $mAdv = new \Think\Model\AdvModel();
        $mAdv->patchQuery($sqls);
        return $this->returnMsg(true);
    }

}
